package com.zmzncs.lmtc.module.park.service.impl;

import com.zmzncs.lmtc.common.pojo.ChnnelsBody;
import com.zmzncs.lmtc.common.pojo.daoer.CreateMonth;
import com.zmzncs.lmtc.common.pojo.daoer.GateOnOff;
import com.zmzncs.lmtc.common.pojo.daoer.MonthRenewal;
import com.zmzncs.lmtc.common.util.DaoerApiUtil;
import com.zmzncs.lmtc.common.util.DateTimeUtil;
import com.zmzncs.lmtc.module.customer.dao.MonthRecordDao;
import com.zmzncs.lmtc.module.customer.pojo.month_record.MonthRecord;
import com.zmzncs.lmtc.module.customer.service.MonthRecordService;
import com.zmzncs.lmtc.module.park.pojo.park_info_daoer.ParkInfoDaoer;
import com.zmzncs.lmtc.module.park.dao.ParkInfoDaoerDao;
import com.zmzncs.lmtc.module.park.service.ParkInfoDaoerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 道尔车场拓展表 服务实现类
 * </p>
 *
 * @author 作者
 * @since 2021-02-05
 */
@Service
public class ParkInfoDaoerServiceImpl extends ServiceImpl<ParkInfoDaoerDao, ParkInfoDaoer> implements ParkInfoDaoerService {

    @Autowired
    ParkInfoDaoerDao parkInfoDaoerDao;
    @Autowired
    MonthRecordService monthRecordService;


    @Override
    public ParkInfoDaoer getInfoByParkId(String parkId) {
        return parkInfoDaoerDao.getInfoByParkId(parkId);
    }


    @Override
    public ChnnelsBody getDaoerChannels(Long parkInfoId) {
        ParkInfoDaoer park = parkInfoDaoerDao.getParkByParkInfoId(parkInfoId);
        if (park != null) {
            //获取车场通道
            ChnnelsBody channels = DaoerApiUtil.getChannels(park.getParkId());
            return channels;
        }
        return null;
    }

    @Override
    public GateOnOff daoerGateOnOff(Long parkInfoId, String channelId, Integer channelIdStatus) {

        ParkInfoDaoer park = parkInfoDaoerDao.getParkByParkInfoId(parkInfoId);
        if (park != null) {
            //道闸开关操作
            GateOnOff gateOnOff = DaoerApiUtil.gateOnOff(park.getParkId(), channelId, channelIdStatus);
            return gateOnOff;
        }
        return null;
    }

    @Override
    public Map<String, String> daoerCreatMonth(CreateMonth createMonth) {
        Map<String, String> map = new HashMap<>();

        //查询停车场信息
        ParkInfoDaoer park = parkInfoDaoerDao.getParkByParkInfoId(createMonth.getParkInfoId());
        if (park == null) {
            map.put("status","0");
            map.put("message","未查询到停车场");
            return map;
        }

        createMonth.setParkId(park.getParkId());
        createMonth.setCardTypeId(createMonth.getCardTypeId());
        createMonth.setPayType(0);
        createMonth.setContactName("停车场主");

        Long parkInfoId = createMonth.getParkInfoId();
        String carNo = createMonth.getCarNo();
        //查询之前是否购买月卡
        List<MonthRecord> records = monthRecordService.getRecordByParkIdAndCarNo(parkInfoId, carNo);
        if (records != null && !records.isEmpty()) {

            MonthRenewal monthRenewal = new MonthRenewal();
            monthRenewal.setParkId(createMonth.getParkId());
            monthRenewal.setCarNo(carNo);
            monthRenewal.setBalanceMoney(createMonth.getBalanceMoney());
            monthRenewal.setNewStartTime(createMonth.getStartTime());
            monthRenewal.setNewEndTime(createMonth.getEndTime());
            monthRenewal.setPayType(1);

            //续卡
            map = DaoerApiUtil.monthRenewal(monthRenewal);
        } else {
            //购买月卡  开户
            map = DaoerApiUtil.creatMonth(createMonth);
        }

        String status = map.get("status");
        if ("1".equals(status)) {
            //保存月租卡开户记录
            int i = monthRecordService.addMonthRecord(createMonth);
        }
        return map;
    }
}
